/**
 * 统一维护渠道快捷操作
 */
import { useRequest } from '@/hooks';
import { message } from 'antd';
import { quickOfflineChannel, quickOnlineChannel, quickReleaseChannel, quickUpdateChannel } from '../service';

function check(res) {
    if (res?.data?.checkType === 1) {
        message.success(res.msg);
    }

    if (res.code !== 10000) {
        message.error(res.msg || '提交失败！');
    }
}

export default function useChannelQuick() {
    const { run: quickOffline } = useRequest(quickOfflineChannel, { manual: true });
    const { run: quickOnline } = useRequest(quickOnlineChannel, { manual: true });
    const { run: quickRelease } = useRequest(quickReleaseChannel, { manual: true });
    const { run: quickUpdate } = useRequest(quickUpdateChannel, { manual: true });

    const update = async params => {
        const res = await quickUpdate(params);
        check(res);
        return res;
    };
    const offline = async params => {
        const res = await quickOffline(params);
        check(res);
        return res;
    };
    const online = async params => {
        const res = await quickOnline(params);
        check(res);
        return res;
    };
    const release = async params => {
        const res = await quickRelease(params);
        check(res);
        return res;
    };
    return { update, offline, online, release };
}
